﻿using System;
using System.Collections.Generic;
using System.Text;
using Server;
using Server.Mobiles;
using Server.Commands;
using Server.Targeting;
using Server.Items;

namespace Bittiez.BackStab
{
    public class BackStab
    {
        public static void Initialize()
        {
            CommandSystem.Register("Backstab", AccessLevel.Player, new CommandEventHandler(BS_OnCommand));
        }
        public static void BS_OnCommand(CommandEventArgs e)
        {
            e.Mobile.SendMessage("Select your target.");
            e.Mobile.Target = new BackstabTarget();
        }
    }





    public class BackstabTarget : Target
    {
        public Mobile f, v;
        public BackstabTarget()
            : base(2, false, TargetFlags.Harmful)
        {
        }
        protected override void OnTarget(Mobile m, object targeted)
        {
            if (targeted is PlayerMobile)
            {
                PlayerMobile vic = (PlayerMobile)targeted;
                Try_BackStab(m, vic);
            }
            else m.SendMessage("You did not target a player");
        }
        public bool Try_BackStab(Mobile from, Mobile Victom)
        {
            if (Check_Directly_Behind(from, Victom))
            {
                if (Check_Stealthed(from, Victom))
                {
                    if (Check_Dagger(from))
                    {
                        Do_Attack(from, Victom);
                        return true;
                    }
                    else from.SendMessage("You must be using a dagger to perform this action.");
                }
                else from.SendMessage("You must be stealthed to perform this action.");
            }
            else from.SendMessage("You must be directly behind your target to perform this action.");
            return false;
        }
        public bool Check_Directly_Behind(Mobile from, Mobile victom)
        {
            int x1, y1, z1, x, y, z;
            x = from.X; y = from.Y; z = from.Z;
            x1 = victom.X; y1 = victom.Y; z1 = victom.Z;
            if (from.Direction == victom.Direction)
            {
                if (from.GetDistanceToSqrt(victom.Location) < 2)
                {
                    switch (victom.Direction)
                    {
                        case Direction.East: x1--; break;
                        case Direction.North: y1++; break;
                        case Direction.South: y1--; break;
                        case Direction.West: x1++; break;
                    }
                    if (x == x1 || y == y1)
                        return true;
                }
            }
            return false;
        }
        public bool Check_Stealthed(Mobile from, Mobile victom)
        {
            PlayerMobile me = (PlayerMobile)from;
            if (me.IsStealthing)
                return true;
            return false;
        }
        public bool Check_Dagger(Mobile from)
        {
            if (from.FindItemOnLayer(Layer.OneHanded) is Dagger)
                return true;
            return false;
        }
        public void Do_Attack(Mobile from, Mobile victom)
        {
            PlayerMobile me = (PlayerMobile)from;
            me.DoHarmful(victom);
            me.Warmode = true;
            me.RevealingAction();
            me.Attack(victom);
            f = me;
            Timer d = Timer.DelayCall(TimeSpan.FromSeconds(0.5), new TimerCallback(DC));
            
            
            me.SendMessage("You backstab your target.");
            victom.SendMessage("You have been backstabbed!");
        }

        public void DC()
        {
            Mobile me = f;
            me.Warmode = false;
            me.Attack(me);
            me.Hidden = true;
        }
    }
}
